package com.a918.util;

import com.a918.entity.MemBwgWithBLOBs;
import com.a918.entity.MemOth;
import com.a918.entity.MemPerWithBLOBs;
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;

import java.io.*;
import java.util.LinkedHashMap;
import java.util.Map;
import java.text.SimpleDateFormat;
public class PDFCreate {

    private static Font titlefont = FontFactory.getFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED,12f, Font.NORMAL, BaseColor.BLACK);
    private static Font textfont = FontFactory.getFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED,8f, Font.NORMAL, BaseColor.BLACK);
//    private static File file = new File("");
//    private static String programepath; //项目路径
//    static {
//        try {
//            programepath = file.getCanonicalPath();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//    }

    public static ByteArrayOutputStream bwgPdf(MemBwgWithBLOBs memBwg){
//        String fullFilePath = programepath+"/src/main/resources/static/file/registerBwg.pdf";
        Document document = new Document(PageSize.A4,15,15,90,15);// 创建文档实例
        ByteArrayOutputStream baos = new ByteArrayOutputStream();

        try{
//            PdfWriter.getInstance(document, new FileOutputStream(fullFilePath));
            PdfWriter.getInstance(document, baos);
            document.open();
            PdfPTable table = new PdfPTable(4);//生成4列的表格
            table.setWidths(new int[] { 3, 5, 3, 5 });//按比例设置宽度
            PdfPCell cell;
            cell = new PdfPCell(new Phrase("全国工业博物馆联盟注册申请表（博物馆会员）", titlefont));
            // 跨四列，由四列变成一列
            cell.setColspan(4);
            //cell.setVerticalAlignment(PdfPCell.ALIGN_CENTER);//设置单元格的垂直对齐方式
            cell.setPaddingTop(6f);
            cell.setPaddingBottom(6f);
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);//设置单元格的水平对齐方式
            cell.setMinimumHeight(24);
            table.addCell(cell);


            // 为了使代码简洁，接下来的存值进行遍历
            Map<String, String> map = new LinkedHashMap<String, String>();
            map.put("博物馆名称", memBwg.getName());
            map.put("馆址", memBwg.getProvince()+memBwg.getCity()+memBwg.getAddress());
            String jibie = memBwg.getMuseumjibie();
            String bwgleixing = memBwg.getMuseumtype();
            if(jibie!=("无")){
                bwgleixing=bwgleixing+"-"+jibie;
            }
            map.put(" 博物馆类型",bwgleixing);
            for (Map.Entry<String, String> entry : map.entrySet()) {
                cell = new PdfPCell(new Phrase(entry.getKey(),textfont));
                cell.setPaddingLeft(6f);
                cell.setPaddingTop(8f);
                cell.setPaddingBottom(8f);
                cell.setMinimumHeight(24);
                table.addCell(cell);
                cell = new PdfPCell(new Phrase(entry.getValue(),textfont));
                cell.setColspan(3);
                cell.setPaddingLeft(6f);
                cell.setPaddingTop(8f);
                cell.setPaddingBottom(8f);
                cell.setMinimumHeight(24);
                table.addCell(cell);
            }

            Map<String, String> map2 = new LinkedHashMap<String, String>();
            map2.put("联系人", memBwg.getLinker());
            map2.put("职务",memBwg.getLinkerjob());
            map2.put("手机",memBwg.getMobilephone());
            map2.put("座机",memBwg.getPhone());
            map2.put("邮箱",memBwg.getEmail());
            map2.put("传真",memBwg.getFax());
            map2.put("所属行业",memBwg.getIndustrytype());
            map2.put("邮编",memBwg.getPostcode());
            map2.put("举办者",memBwg.getOrganizer());
            map2.put("是否有独立法人资格",memBwg.getIsfreeopen()==1?"是":"否");
            map2.put("统一社会信用代码",memBwg.getCreditcode());
            map2.put("法定代表人",memBwg.getFaren());
            map2.put("馆舍面积(平方米)",String.valueOf(memBwg.getBuildingarea()));
            map2.put("展示面积(平方米)",String.valueOf(memBwg.getShowarea()));
            map2.put("藏品数量(件)",String.valueOf(memBwg.getCangpinnum()));
            map2.put("从业人数",String.valueOf(memBwg.getEmployeenum()));
            map2.put("全年开放天数",String.valueOf(memBwg.getOpendays()));
            map2.put("是否免费开放",memBwg.getIsfreeopen()==1?"是":"否");
            map2.put("上一年参观人次(万)",String.valueOf(memBwg.getVistornum()));
            map2.put("上一年举办活动次数",String.valueOf(memBwg.getPropagandanum()));

            for (Map.Entry<String, String> entry2 : map2.entrySet()) {
                cell = new PdfPCell(new Phrase(entry2.getKey(),textfont));
                //cell.setVerticalAlignment(PdfPCell.ALIGN_CENTER);//设置单元格的垂直对齐方式
                cell.setPaddingLeft(6f);
                cell.setPaddingTop(8f);
                cell.setPaddingBottom(8f);
                cell.setMinimumHeight(24);
                table.addCell(cell);
                cell = new PdfPCell(new Phrase(entry2.getValue(),textfont));
                cell.setPaddingLeft(6f);
                cell.setPaddingTop(8f);
                cell.setPaddingBottom(8f);
                cell.setMinimumHeight(24);
                table.addCell(cell);
            }

            cell = new PdfPCell(new Phrase("单位简介", textfont));
            cell.setPaddingLeft(6f);
            cell.setPaddingTop(16f);
            cell.setPaddingBottom(16f);
            cell.setMinimumHeight(40);
            table.addCell(cell);
            cell = new PdfPCell(new Phrase(memBwg.getUnitintroduction(), textfont));
            cell.setPaddingLeft(6f);
            cell.setColspan(3);
            cell.setPaddingTop(8f);
            cell.setMinimumHeight(40);
            table.addCell(cell);

            cell = new PdfPCell(new Phrase("申请申明", textfont));
            cell.setPaddingLeft(6f);
            cell.setPaddingTop(21f);
            cell.setPaddingBottom(21f);
            cell.setMinimumHeight(50);
            table.addCell(cell);
            cell = new PdfPCell(new Phrase("本单位保证以上所填内容属实，并对提交材料的真实性、有效性和合法性承担相应责任。\n\n\n(签字/盖章)", textfont));
            cell.setColspan(3);
            cell.setPaddingLeft(6f);
            cell.setPaddingTop(8f);
            cell.setVerticalAlignment(PdfPCell.ALIGN_CENTER);//设置单元格的垂直对齐方式
            cell.setMinimumHeight(50);
            table.addCell(cell);

            cell = new PdfPCell(new Phrase("备注", textfont));
            cell.setPaddingLeft(6f);
            cell.setPaddingTop(23f);
            cell.setPaddingBottom(21f);
            cell.setMinimumHeight(50);
            table.addCell(cell);
            cell = new PdfPCell(new Phrase("1.请申请单位在纸质版表中签字、盖章，并生成pdf格式的扫描件。\n\n2.请另附组织机构代码证或其他合法性证明（原件）的pdf格式扫描件。\n\n3.以上两种文件上传为审核附件。", textfont));
            cell.setColspan(3);
            cell.setPaddingLeft(6f);
            cell.setPaddingTop(9f);
            cell.setPaddingBottom(13f);
            cell.setMinimumHeight(50);
            table.addCell(cell);
            document.add(table);
        }catch (DocumentException  e) {
            e.printStackTrace();
        } finally {
            document.close();
        }
        return baos;
    }


    public static ByteArrayOutputStream perPdf(MemPerWithBLOBs memPer){
//        String fullFilePath = programepath+"/src/main/resources/static/file/registerBwg.pdf";
        Document document = new Document(PageSize.A4,15,15,90,15);// 创建文档实例
        ByteArrayOutputStream baos = new ByteArrayOutputStream();

        try{
//            PdfWriter.getInstance(document, new FileOutputStream(fullFilePath));
            PdfWriter.getInstance(document, baos);
            document.open();
            PdfPTable table = new PdfPTable(4);//生成4列的表格
            table.setWidths(new int[] { 3, 5, 3, 5 });//按比例设置宽度
            PdfPCell cell;
            cell = new PdfPCell(new Phrase("全国工业博物馆联盟注册申请表（个人会员）", titlefont));
            // 跨四列，由四列变成一列
            cell.setColspan(4);
            //cell.setVerticalAlignment(PdfPCell.ALIGN_CENTER);//设置单元格的垂直对齐方式
            cell.setPaddingTop(6f);
            cell.setPaddingBottom(6f);
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);//设置单元格的水平对齐方式
            cell.setMinimumHeight(24);
            table.addCell(cell);


            // 为了使代码简洁，接下来的存值进行遍历

            Map<String, String> map2 = new LinkedHashMap<String, String>();
            map2.put("姓名", memPer.getName());
            map2.put("性别",String.valueOf(memPer.getGender()));
            String dateStr = new SimpleDateFormat("yyyy-MM-dd").format(memPer.getBorndate());
            map2.put("出生日期",dateStr);
            map2.put("职称",memPer.getJobtitle());
            map2.put("现任职务",memPer.getPosition());
            map2.put("所在行业",memPer.getIndustrytype());
            map2.put("学历",memPer.getXueli());
            map2.put("毕业院校",memPer.getEducateschool());
            map2.put("所学专业",memPer.getSpecialty());
            map2.put("身份证号",memPer.getIdcard());
            map2.put("电话",memPer.getPhone());
            map2.put("邮箱",memPer.getEmail());

            for (Map.Entry<String, String> entry2 : map2.entrySet()) {
                cell = new PdfPCell(new Phrase(entry2.getKey(),textfont));
                //cell.setVerticalAlignment(PdfPCell.ALIGN_CENTER);//设置单元格的垂直对齐方式
                cell.setPaddingLeft(6f);
                cell.setPaddingTop(8f);
                cell.setPaddingBottom(8f);
                cell.setMinimumHeight(24);
                table.addCell(cell);
                cell = new PdfPCell(new Phrase(entry2.getValue(),textfont));
                cell.setPaddingLeft(6f);
                cell.setPaddingTop(8f);
                cell.setPaddingBottom(8f);
                cell.setMinimumHeight(24);
                table.addCell(cell);
            }

            cell = new PdfPCell(new Phrase("工作经历", textfont));
            cell.setPaddingLeft(6f);
            cell.setPaddingTop(20f);
            cell.setPaddingBottom(20f);
            cell.setMinimumHeight(48);
            table.addCell(cell);
            cell = new PdfPCell(new Phrase(memPer.getWorkexperience(), textfont));
            cell.setPaddingLeft(6f);
            cell.setColspan(3);
            cell.setPaddingTop(6f);
            cell.setMinimumHeight(48);
            table.addCell(cell);

            cell = new PdfPCell(new Phrase("工作成果", textfont));
            cell.setPaddingLeft(6f);
            cell.setPaddingTop(20f);
            cell.setPaddingBottom(20f);
            cell.setMinimumHeight(48);
            table.addCell(cell);
            cell = new PdfPCell(new Phrase(memPer.getWorkresult(), textfont));
            cell.setPaddingLeft(6f);
            cell.setColspan(3);
            cell.setPaddingTop(6f);
            cell.setMinimumHeight(48);
            table.addCell(cell);

            cell = new PdfPCell(new Phrase("所获荣誉", textfont));
            cell.setPaddingLeft(6f);
            cell.setPaddingTop(20f);
            cell.setPaddingBottom(20f);
            cell.setMinimumHeight(48);
            table.addCell(cell);
            cell = new PdfPCell(new Phrase(memPer.getHonor(), textfont));
            cell.setPaddingLeft(6f);
            cell.setColspan(3);
            cell.setPaddingTop(6f);
            cell.setMinimumHeight(48);
            table.addCell(cell);



            cell = new PdfPCell(new Phrase("申请单位", textfont));
            cell.setPaddingLeft(6f);
            cell.setPaddingTop(20f);
            cell.setPaddingBottom(20f);
            cell.setMinimumHeight(48);
            table.addCell(cell);
            cell = new PdfPCell(new Phrase("本人保证以上所填内容属实，并对提交材料的真实性、有效性和合法性承担相应责任。\n\n(签字/盖章)", textfont));
            cell.setColspan(3);
            cell.setPaddingLeft(6f);
            cell.setPaddingTop(10f);
            cell.setVerticalAlignment(PdfPCell.ALIGN_CENTER);//设置单元格的垂直对齐方式
            cell.setMinimumHeight(48);
            table.addCell(cell);

            cell = new PdfPCell(new Phrase("备注", textfont));
            cell.setPaddingLeft(6f);
            cell.setPaddingTop(8f);
            cell.setPaddingBottom(8f);
            cell.setMinimumHeight(24);
            table.addCell(cell);
            cell = new PdfPCell(new Phrase("本表格可导出打印，请申请单位在纸质版表中盖章、签字并上传图片为附件。", textfont));
            cell.setColspan(3);
            cell.setPaddingLeft(6f);
            cell.setPaddingTop(8f);
            cell.setPaddingBottom(8f);
            cell.setMinimumHeight(24);


            table.addCell(cell);
            document.add(table);
        }catch (DocumentException  e) {
            e.printStackTrace();
        } finally {
            document.close();
        }
        return baos;
    }


    public static ByteArrayOutputStream othPdf(MemOth memOth){
//        String fullFilePath = programepath+"/src/main/resources/static/file/registerBwg.pdf";
        Document document = new Document(PageSize.A4,15,15,90,15);// 创建文档实例
        ByteArrayOutputStream baos = new ByteArrayOutputStream();

        try{
//            PdfWriter.getInstance(document, new FileOutputStream(fullFilePath));
            PdfWriter.getInstance(document, baos);
            document.open();
            PdfPTable table = new PdfPTable(4);//生成4列的表格
            table.setWidths(new int[] { 3, 5, 3, 5 });//按比例设置宽度
            PdfPCell cell;
            cell = new PdfPCell(new Phrase("全国工业博物馆联盟注册申请表（其他单位会员）", titlefont));
            // 跨四列，由四列变成一列
            cell.setColspan(4);
            //cell.setVerticalAlignment(PdfPCell.ALIGN_CENTER);//设置单元格的垂直对齐方式
            cell.setPaddingTop(6f);
            cell.setPaddingBottom(6f);
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);//设置单元格的水平对齐方式
            cell.setMinimumHeight(24);
            table.addCell(cell);


            // 为了使代码简洁，接下来的存值进行遍历

            Map<String, String> map2 = new LinkedHashMap<String, String>();
            map2.put("姓名", memOth.getName());
            map2.put("法人",memOth.getFaren());
            map2.put("联系人",memOth.getLinker());
            map2.put("职务",memOth.getLinkerjob());
            map2.put("传真",memOth.getFax());
            map2.put("邮编",memOth.getEmail());
            map2.put("座机",memOth.getPhone());
            map2.put("手机",memOth.getMobilephone());
            map2.put("邮箱",memOth.getEmail());
            String dateStr = new SimpleDateFormat("yyyy-MM-dd").format(memOth.getCreattime());
            map2.put("成立时间",dateStr);
            map2.put("注册资本(万元)",String.valueOf(memOth.getRegisterasset()));
            map2.put("从业人数",String.valueOf(memOth.getEmployeenum()));
            map2.put("单位类型",String.valueOf(memOth.getUnittype()));
            map2.put("统一社会信用代码",String.valueOf(memOth.getCreditcode()));


            for (Map.Entry<String, String> entry2 : map2.entrySet()) {
                cell = new PdfPCell(new Phrase(entry2.getKey(),textfont));
                //cell.setVerticalAlignment(PdfPCell.ALIGN_CENTER);//设置单元格的垂直对齐方式
                cell.setPaddingLeft(6f);
                cell.setPaddingTop(8f);
                cell.setPaddingBottom(8f);
                cell.setMinimumHeight(24);
                table.addCell(cell);
                cell = new PdfPCell(new Phrase(entry2.getValue(),textfont));
                cell.setPaddingLeft(6f);
                cell.setPaddingTop(8f);
                cell.setPaddingBottom(8f);
                cell.setMinimumHeight(24);
                table.addCell(cell);
            }

            cell = new PdfPCell(new Phrase("业务范围", textfont));
            cell.setPaddingLeft(6f);
            cell.setPaddingTop(8f);
            cell.setPaddingBottom(8f);
            cell.setMinimumHeight(24);
            table.addCell(cell);
            cell = new PdfPCell(new Phrase(memOth.getBusinessrange(), textfont));
            cell.setPaddingLeft(6f);
            cell.setColspan(3);
            cell.setPaddingTop(8f);
            cell.setMinimumHeight(24);
            table.addCell(cell);

            cell = new PdfPCell(new Phrase("地址", textfont));
            cell.setPaddingLeft(6f);
            cell.setPaddingTop(8f);
            cell.setPaddingBottom(8f);
            cell.setMinimumHeight(24);
            table.addCell(cell);
            cell = new PdfPCell(new Phrase(memOth.getProvince()+memOth.getCity()+memOth.getAddress(), textfont));
            cell.setPaddingLeft(6f);
            cell.setColspan(3);
            cell.setPaddingTop(8f);
            cell.setMinimumHeight(24);
            table.addCell(cell);

            cell = new PdfPCell(new Phrase("单位介绍", textfont));
            cell.setPaddingLeft(6f);
            cell.setPaddingTop(20f);
            cell.setPaddingBottom(20f);
            cell.setMinimumHeight(48);
            table.addCell(cell);
            cell = new PdfPCell(new Phrase(memOth.getGroupintroduct(), textfont));
            cell.setPaddingLeft(6f);
            cell.setColspan(3);
            cell.setPaddingTop(6f);
            cell.setMinimumHeight(48);
            table.addCell(cell);

            cell = new PdfPCell(new Phrase("申请申明", textfont));
            cell.setPaddingLeft(6f);
            cell.setPaddingTop(31f);
            cell.setPaddingBottom(31f);
            cell.setMinimumHeight(70);
            table.addCell(cell);
            cell = new PdfPCell(new Phrase("本单位保证以上所填内容属实，并对提交材料的真实性、有效性和合法性承担相应责任。\n\n\n\n\n\n(签字/盖章)", textfont));
            cell.setColspan(3);
            cell.setPaddingLeft(6f);
            cell.setPaddingTop(8f);
            cell.setVerticalAlignment(PdfPCell.ALIGN_CENTER);//设置单元格的垂直对齐方式
            cell.setMinimumHeight(70);
            table.addCell(cell);

            cell = new PdfPCell(new Phrase("备注", textfont));
            cell.setPaddingLeft(6f);
            cell.setPaddingTop(23f);
            cell.setPaddingBottom(21f);
            cell.setMinimumHeight(50);
            table.addCell(cell);
            cell = new PdfPCell(new Phrase("1.请申请单位在纸质版表中签字、盖章，并生成pdf格式的扫描件。\n\n2.请另附组织机构代码证或其他合法性证明（原件）的pdf格式扫描件。\n\n3.以上两种文件上传为审核附件。", textfont));
            cell.setColspan(3);
            cell.setPaddingLeft(6f);
            cell.setPaddingTop(9f);
            cell.setPaddingBottom(13f);
            cell.setMinimumHeight(50);
            table.addCell(cell);
            document.add(table);
        }catch (DocumentException  e) {
            e.printStackTrace();
        } finally {
            document.close();
        }
        return baos;
    }




}